<?php

namespace mbilling\BillingBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;

use mbilling\BillingBundle\Model\OrdersOSModel;
use mbilling\BillingBundle\Model\UsersAdminLogsModel;
use mbilling\BillingBundle\Entity\ordersOS;
use mbilling\BillingBundle\Entity\usersAdminLogs;

use mbilling\BillingBundle\Form\OrdersOSForm;

class OrdersOSController extends Controller
{
    public function indexAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $os_model = new OrdersOSModel($em);

        $os = $os_model->findAll();
        return $this->render('BillingBundle:Orders_OS:index.html.twig', array('oss' => $os));
    }
    public function addAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $os_model = new OrdersOSModel($em);
        $os = new ordersOS();
        //$os_model->create($os->initDefaultEntity());
        //return new RedirectResponse($this->generateUrl('orders_os_show'));
        $form = $this->createForm(new OrdersOSForm, $os);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $os_model->create($form->getData());
                $securityContext = $this->container->get('security.context');
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('8');//страница операционные системы
                $log->setObjectId($os->getId());//изменяемый объект
                $log->setActionTypeId('1');//создание ОС
                $log_model->create($log);
                return new RedirectResponse($this->generateUrl('orders_os_show'));
            }
        }
        return $this->render('BillingBundle:Orders_OS:create.html.twig', array('form' => $form->createView()));
    }
    public function updateAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $os_model = new OrdersOSModel($em);
        $os = $os_model->findByPK($id);

        $form = $this->createForm(new OrdersOSForm, $os);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $os_model->create($form->getData());
                $securityContext = $this->container->get('security.context');
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('8');//страница операционные системы
                $log->setObjectId($os->getId());//изменяемый объект
                $log->setActionTypeId('2');//изменение ОС
                $log_model->create($log);
                return new RedirectResponse($this->generateUrl('orders_os_show'));
            }
        }
        return $this->render('BillingBundle:Orders_OS:create.html.twig', array('form' => $form->createView()));
    }
    public function removeAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $os_model = new OrdersOSModel($em);
        $os_model->removeByPK($id);

        $securityContext = $this->container->get('security.context');
        $userCurrent = $securityContext->getToken()->getUser();

        $log_model = new UsersAdminLogsModel($em);
        $log = new usersAdminLogs();
        $log->setAdminId($userCurrent->getId());
        $log->setPageId('8');//страница операционные системы
        $log->setObjectId($id);//изменяемый объект
        $log->setActionTypeId('3');//удаление ОС
        $log_model->create($log);
        return new RedirectResponse($this->generateUrl('orders_os_show'));
    }
}